import { defineStore } from 'pinia'
import { defaultSettings } from './app-state'
import { getMenuList } from '@/api/user'
import { getUserButtonPermission } from '@/api/system'
import { getFormatDynamicRouter } from '@/utils/common/dynamic-router'
import router from '@/router'

import { myRoutes } from '@/router/router'

//首页
import home from '@/layout/index.vue'
import { CodeEnum } from '@/constants/http-code'

const useAppStore = defineStore('app', {
  state: () => {
    return defaultSettings
  },

  actions: {
    /**
     * 设置菜单宽
     * @param width 菜单框
     */
    setMenuWidth(width: number) {
      this.menuWidth = width
    },

    /**
     * 获取后台路由数据
     */
    async getServerMenuList() {
      // const res = await getMenuList()

      // if (res.code != 200) {
      //   //throw new Error('获取用户菜单异常')
      //   return
      // }

      // //处理后端菜单数据
      // const data = await getFormatDynamicRouter(res.data)

      //添加动态路由
      router.addRoute({
        path: '/',
        component: home,
        redirect: '/dashboard/data/workplace',
        children: myRoutes,
      })

      //保存路由数据
      this.serverMenu = myRoutes
    },

    /**
     * 请求用户权限
     */
    async getServicePermissionList() {
      const res = await getUserButtonPermission()
      this.permissionList = res.data
    },
  },
})

export default useAppStore
